package com.miyu.book.utils.bookUtils;

import com.alibaba.fastjson.JSONObject;
import com.miyu.book.dao.bookMagic.BookMagicDao;
import com.miyu.book.utils.magic.GetBeanUtil;


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

public class InitSaveThead implements Callable<JSONObject> {
    private JSONObject data;
    BookMagicDao dao = (BookMagicDao) GetBeanUtil.getBeanByClass(BookMagicDao.class);
    @Override
    public JSONObject call() throws Exception {

        if (!data.isEmpty()) {
            if(data.getString("type").equals("initData")){
                JSONObject bookInfo = data.getJSONObject("bookInfo");
                String ruleId = bookInfo.getString("ruleId");
                String bookTable = "book_"+ruleId;
                String chTable = "chapter_"+ruleId + "_" + BookCommutils.getTable();//小说章节表
                StringBuffer sql = new StringBuffer();
                sql.append(" insert into ").append(bookTable).append(" (book_id,rule_id,book_source,book_name,book_chapter_source,book_author,book_kind,book_state,book_cover,book_duce,book_last_chapter");
                sql.append(" ,book_last_chapter_url,book_last_chapter_date) ");
                sql.append(" values (");
                sql.append("'").append(bookInfo.getString("bookId")).append("',");
                sql.append("'").append(bookInfo.getString("ruleId")).append("',");
                sql.append("'").append(bookInfo.getString("bookUrl")).append("',");
                sql.append("'").append(bookInfo.getString("bookName")).append("',");
                sql.append("'").append(chTable).append("',");
                if (bookInfo.containsKey("bookAuthor"))
                    sql.append("'").append(bookInfo.getString("bookAuthor")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookKind"))
                    sql.append("'").append(bookInfo.getString("bookKind")).append("',");
                else
                    sql.append(" null,");

                if (bookInfo.containsKey("bookState"))
                    sql.append("'").append(bookInfo.getString("bookState")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookCover"))
                    sql.append("'").append(bookInfo.getString("bookCover")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookDuce"))
                    sql.append("'").append(bookInfo.getString("bookDuce")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookLastChapter"))
                    sql.append("'").append(bookInfo.getString("bookLastChapter")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookLastChapterUrl"))
                    sql.append("'").append(bookInfo.getString("bookLastChapterUrl")).append("',");
                else
                    sql.append(" null,");
                if (bookInfo.containsKey("bookLastChapterDate"))
                    sql.append("'").append(bookInfo.getString("bookLastChapterDate")).append("')");
                else
                    sql.append(" null)");

               // dao.doExecute(sql.toString());//保存小说明细
                List<String> sqlList = new ArrayList<>();
                sqlList.add(sql.toString());
                JSONObject chapterJson = data.getJSONObject("chapterInfo");
                StringBuffer sqlCh = new StringBuffer();
    //            sqlCh.append(" insert into ").append(chTable).append(" ( chapter_id,book_id,chapter_source,chapter_num,chapter_name,chapter_context)");
    //            sqlCh.append(" values ");
                for (Map.Entry entry : chapterJson.entrySet()) {
                    JSONObject ch = (JSONObject) entry.getValue();
                    sqlCh.setLength(0);
                    sqlCh.append(" insert into ").append(chTable).append(" ( chapter_id,book_id,chapter_source,chapter_num,chapter_name,chapter_context)");
                    sqlCh.append(" values ");
                    sqlCh.append("  (");
                    sqlCh.append("'").append(ch.getString("chapterId")).append("',");
                    sqlCh.append("'").append(ch.getString("bookId")).append("',");
                    sqlCh.append("'").append(ch.getString("chapterUrl")).append("',");
                    sqlCh.append("'").append(ch.getString("num")).append("',");
                    sqlCh.append("'").append(ch.getString("chapterName")).append("',");
                    sqlCh.append("null)");
                    sqlList.add(sqlCh.toString());
                }
    //            sqlCh.deleteCharAt(sqlCh.length()-1);
    //            sqlList.add(sqlCh.toString());
                if (sqlList.size()>0){
                   dao.doExecuteBatch(sqlList);
                    sqlList.clear();
                }

            }else if(data.getString("type").equals("initDataChapter")){
                StringBuffer sql = new StringBuffer();
                sql.append("update ").append(data.getString("tableId"));
                sql.append(" set chapter_context = '").append(data.getString("chapterContext")).append("'");
                sql.append(" where chapter_id = '").append(data.getString("chapterId")).append("'");
                dao.doExecute(sql.toString());
                data.clear();

            }
        }
        return null;
    }


    public  InitSaveThead(JSONObject data){
        this.data=data;
    }
}
